using System.IO;
using UnityEngine;

public class ControllerData : UDPMessageBase
{
	public Quaternion attitude;

	public Vector3 gravity;

	public Vector3 rotationRate;

	public Vector3 rotationRateUnbiased;

	public int screenOrientation;

	public float updateInterval;

	public Vector3 userAcceleration;

	public float headingAccuracy;

	public float magneticHeading;

	public Vector3 rawMagneticVector;

	public double magneticTimestamp;

	public float trueHeading;

	public bool isUserTouchingSurface;

	public Vector3 touchPosition;

	public ControllerData()
	{
		type = MessageType.ControllerDataMessage;
	}

	public override void Serialize(BinaryWriter writer)
	{
		base.Serialize(writer);
		SendQuaternion(writer, attitude);
		SendVector(writer, gravity);
		SendVector(writer, rotationRate);
		SendVector(writer, rotationRateUnbiased);
		writer.Write(screenOrientation);
		writer.Write(updateInterval);
		SendVector(writer, userAcceleration);
		writer.Write(headingAccuracy);
		writer.Write(magneticHeading);
		SendVector(writer, rawMagneticVector);
		writer.Write(magneticTimestamp);
		writer.Write(trueHeading);
		writer.Write(isUserTouchingSurface);
		SendVector(writer, touchPosition);
	}

	private void SendVector(BinaryWriter writer, Vector3 vec)
	{
		writer.Write(vec.x);
		writer.Write(vec.y);
		writer.Write(vec.z);
	}

	private void SendQuaternion(BinaryWriter writer, Quaternion quat)
	{
		writer.Write(quat.w);
		writer.Write(quat.x);
		writer.Write(quat.y);
		writer.Write(quat.z);
	}

	public override void Deserialize(BinaryReader reader)
	{
		base.Deserialize(reader);
		attitude = GetQuaternion(reader);
		gravity = GetVector(reader);
		rotationRate = GetVector(reader);
		rotationRateUnbiased = GetVector(reader);
		screenOrientation = reader.ReadInt32();
		updateInterval = reader.ReadSingle();
		userAcceleration = GetVector(reader);
		headingAccuracy = reader.ReadSingle();
		magneticHeading = reader.ReadSingle();
		rawMagneticVector = GetVector(reader);
		magneticTimestamp = reader.ReadDouble();
		trueHeading = reader.ReadSingle();
		isUserTouchingSurface = reader.ReadBoolean();
		touchPosition = GetVector(reader);
	}

	private Vector3 GetVector(BinaryReader reader)
	{
		float x = reader.ReadSingle();
		float y = reader.ReadSingle();
		float z = reader.ReadSingle();
		return new Vector3(x, y, z);
	}

	private Quaternion GetQuaternion(BinaryReader reader)
	{
		float w = reader.ReadSingle();
		float x = reader.ReadSingle();
		float y = reader.ReadSingle();
		float z = reader.ReadSingle();
		return new Quaternion(x, y, z, w);
	}
}
